package com.liuweiheng.base.utils;

import cn.hutool.core.util.StrUtil;

/**
 * 字符串工具类
 * @author lwh
 * @date 2021/11/18
 */
public class StringUtil {
    private StrUtil util;

    public static boolean isEmpty(String str) {
        int length;
        if (str != null && (length = str.length()) != 0) {
            for (int i = 0; i < length; i++) {
                char c = str.charAt(i);
                if (!(Character.isWhitespace(c) || Character.isSpaceChar(c) || c == 65279 || c == 8234)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isEquals(String str1, String str2, boolean ignoreCase) {
        if (null == str1) {
            return str2 == null;
        } else if (str2 == null) {
            return false;
        } else {
            return ignoreCase ? str1.equalsIgnoreCase(str2) : str1.contentEquals(str2);
        }
    }

    public static String replace(String str, String searchStr, String replaceStr) {
        if (!isEmpty(str) && !isEmpty(searchStr)) {
            if (null == replaceStr) {
                replaceStr = "";
            }
            int strLength = str.length();
            int searchStrLength = searchStr.length();
            StringBuilder result = new StringBuilder(strLength + 16);
            int preIndex;
            int index;
            for (preIndex = 0; (index = str.indexOf(searchStr)) > -1; preIndex = index + searchStrLength) {
                result.append(str.subSequence(preIndex, index));
                result.append(replaceStr);
            }
            if (preIndex < strLength) {
                result.append(str.subSequence(preIndex, strLength));
            }
            return result.toString();
        } else {
            return str;
        }
    }

}
